package cn.wolfcode.controller;


import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.*;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {
@Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IConsumptionService consumptionService;
@Autowired
    private IBusinessService businessService;
@Autowired
private ISystemDictionaryItemService systemDictionaryItemService;
@Autowired
private IConsumptionItemService consumptionItemService;


    @RequiredPermission(name="结算单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "consumption/list";
    }


    @RequiredPermission(name="结算单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long consumptionId) {
        if (consumptionId != null) {
           consumptionService.delete(consumptionId);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        model.addAttribute("categoryItem",systemDictionaryItemService.queryBySn("business"));
        model.addAttribute("payTypeItem",systemDictionaryItemService.queryBySn("pay_type"));
        model.addAttribute("businesses",businessService.listAll());
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
           /* Employee employee = employeeService.selectById(UserContext.getCurrentUser().getId());
            consumption.setPayee(employee);*/
            model.addAttribute("consumption",consumption);

          List<ConsumptionItem> items= consumptionItemService.queryByCno(consumption.getCno());
          model.addAttribute("items",items);
        }
        return "consumption/input";
    }
@RequestMapping("/save")
  public String save(Long appointmentId,Model model){
        //创建好新的结算单将预约单的信息封装到结算单对象中
   Long consumptionId =  consumptionService.saveFormAppointment(appointmentId);
    //然后重定向到input方法里,地址带上创建的结算单对象的id
    return "redirect:/consumption/input?id="+consumptionId;

  }
@RequestMapping("/updateStatus")
public String updateStatus(Long consumptionId,Integer status,Model model){
    Consumption consumption = consumptionService.get(consumptionId);
    System.out.println(consumption);
          consumption.setStatus(status);
    Date now = new Date();
    consumption.setPayTime(now);
    consumption.setPayee(UserContext.getCurrentUser());
    consumption.setCheckoutTime(now);
    consumptionService.updateStatus(consumption);
    /*
    *
    **/
    System.out.println(consumptionId);

    return"redirect:/consumption/input?id="+consumption.getId();
}

}
